# -*- coding:utf-8 -*-
import json
import common
import requests


class Earthquake:
    DataSources = 0
    Time = ''
    Level = 0
    Depth = 0
    Longitude = 0
    Latitude = 0
    Location = ''
    Address = ''
    Country = ''
    Province = ''
    City = ''
    District = ''
    Town = ''
    CountryCode = 0
    CityCode = 0
    CityLevel = 0
    TownCode = ''
    AdCode = ''

    def __init__(self, sources, time, level, depth, longitude, latitude, location, address=None, country=None, province=None, city=None, district=None, town=None, country_code=None, city_code=None, city_level=None, town_code=None, ad_code=None):
        """
        地震数据实体类
        :param sources: 数据来源
        :param time: 地震时间
        :param level: 震级
        :param depth: 地震深度
        :param longitude: 经度
        :param latitude: 纬度
        :param location: 地点
        :param address: 地址
        :param country: 国家
        :param province: 省份
        :param city: 城市
        :param district: 县
        :param town: 乡镇
        :param country_code: 国家代码
        :param city_code: 城市代码
        :param city_level: 城市水平
        :param town_code: 乡镇代码
        :param ad_code:  行政区划代码
        """
        self.DataSources = sources
        self.Time = time
        self.Level = level
        self.Depth = depth
        self.Longitude = longitude
        self.Latitude = latitude
        self.Location = location if location else ''
        if address and country and province and city and district and town and country_code and city_code and city_level and town_code and ad_code:
            self.Address = address
            self.Country = country
            self.Province = province
            self.City = city
            self.District = district
            self.Town = town
            self.CountryCode = country_code
            self.CityCode = city_code
            self.CityLevel = city_level
            self.TownCode = town_code
            self.AdCode = ad_code

    def set_address(self):
        config = common.get_config()
        url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak={}&output=json&coordtype={}&location={},{}&extensions_town=true&language=zh-C'
        url = url.format(config['ak'], config['coord_type'], self.Latitude, self.Longitude)
        res = requests.get(url)
        if res.status_code == 200:
            data = json.loads(res.text)
            if data.get('status', -1) == 0:
                self.Address = data['result']['formatted_address']
                self.Country = data['result']['addressComponent']['country']
                self.Province = data['result']['addressComponent']['province']
                self.City = data['result']['addressComponent']['city']
                self.District = data['result']['addressComponent']['district']
                self.Town = data['result']['addressComponent']['town']
                self.CountryCode = data['result']['addressComponent']['country_code']
                self.CityCode = data['result']['cityCode']
                self.CityLevel = data['result']['addressComponent']['city_level']
                self.TownCode = data['result']['addressComponent']['town_code']
                self.AdCode = data['result']['addressComponent']['adcode']
                return
        raise Exception('逆地理编码地址获取失败')

    def __cmp__(self, other):
        for i in self.__dict__:
            if getattr(self, str(i)) != getattr(other, str(i)):
                return False
        return True

    def __str__(self):
        s = 'source:{}\ttime:{}\tlevel:{}\tdepth:{}\tlon:{}\tlat:{}\tlocation:{}'
        return s.format(self.DataSources, self.Time, self.Level, self.Depth, self.Longitude, self.Latitude, self.Location)
